//
//  GQJProgressView.m
//  TotalDemo
//
//  Created by gujian on 16/6/28.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJProgressView.h"

@interface GQJProgressView()
{
    CGFloat xProgress;
}

@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) UIColor *backColor;
@property (nonatomic, strong) UIColor *beignColor;
@property (nonatomic, strong) UIColor *endColor;

@end

@implementation GQJProgressView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initParams];
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initParams];
    }
    
    return self;
}

- (void)initParams
{
    self.backgroundColor = [UIColor clearColor];
    _backColor = UICOLOR_ARGB(0xffd8d8d8);
    _beignColor = UICOLOR_ARGB(0xfffad963);
    _endColor = UICOLOR_ARGB(0xfff76c1d);
    xProgress = 1.5;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
//    [super drawRect:rect];
    
    if (!rect.size.width || !rect.size.height) {
        return;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSaveGState(context);
    
    //抗锯齿
    CGContextSetAllowsAntialiasing(context, true);
    CGFloat radius = 0.5f * rect.size.height;
    
    //绘制背景色
    [_backColor setFill];
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect));
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMidX(rect), CGRectGetMinY(rect), radius);
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMidY(rect), radius);
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMidX(rect), CGRectGetMaxY(rect), radius);
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMidY(rect), radius);
    CGContextClosePath(context);
    CGContextFillPath(context);
    
    
    //绘制前景色
    rect.size.width *= _progress;
    if (rect.size.width >.0 && rect.size.width < 2 * radius) {
        rect.size.width = 2 * radius ;
    }
    

    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMidY(rect));
    CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMidX(rect), CGRectGetMinY(rect), radius);
    CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMidY(rect), radius);
    CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMidX(rect), CGRectGetMaxY(rect), radius);
    CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMidY(rect), radius);
    CGPathCloseSubpath(path);
    [self drawLinerGradient:context path:path startColor:_beignColor.CGColor endColor:_endColor.CGColor];
    
    CGContextRestoreGState(context);
    
    CGPathRelease(path);
    
}



- (void)drawLinerGradient:(CGContextRef)context path:(CGPathRef)path startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = {0.0, 1.0};
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    CGRect pathRect = CGPathGetBoundingBox(path);
    
    CGPoint startPoint = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMidY(pathRect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMidY(pathRect));
    
    CGContextSaveGState(context);
    
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    
    CGContextRestoreGState(context);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    
}


- (void)updateWithProgress:(CGFloat)progress
{
    if (progress < 0) {
        progress = 0;
    }
    
    if (progress > 1) {
        progress = 1;
    }
    
    _progress = progress;
    [self setNeedsDisplay];
}

#pragma mark - KeyValueCoding

//使用 validateValue:forKey:error:时需要实现此方法
- (BOOL)validateXProgress:(id *)value error:(NSError **)error
{
    if ([*value isKindOfClass:[NSObject class]]) {
        
        *error = [NSError errorWithDomain:@"MyErrorDomain" code:1 userInfo:nil];
        
        return NO;
    }
    
    
    return YES;
}

@end



























